package com.quick.DP.strategyMode;

import java.util.HashMap;
import java.util.Map;

// 策略接口
interface DiscountStrategy {
    double calculate(double amount);
    String getMemberLevel();
}

// 普通会员策略
class NormalStrategy implements DiscountStrategy {
    @Override
    public double calculate(double amount) {
        System.out.println("普通会员无折扣");
        return amount;
    }
    
    @Override
    public String getMemberLevel() {
        return "normal";
    }
}

// 银牌会员策略
class SilverStrategy implements DiscountStrategy {
    @Override
    public double calculate(double amount) {
        System.out.println("银牌会员9折");
        return amount * 0.9;
    }
    
    @Override
    public String getMemberLevel() {
        return "silver";
    }
}

// 金牌会员策略
class GoldStrategy implements DiscountStrategy {
    @Override
    public double calculate(double amount) {
        System.out.println("金牌会员8折");
        return amount * 0.8;
    }
    
    @Override
    public String getMemberLevel() {
        return "gold";
    }
}

// 白金会员策略
class PlatinumStrategy implements DiscountStrategy {
    @Override
    public double calculate(double amount) {
        System.out.println("白金会员7折");
        return amount * 0.7;
    }
    
    @Override
    public String getMemberLevel() {
        return "platinum";
    }
}

// 策略工厂
class DiscountStrategyFactory {
    private static final Map<String, DiscountStrategy> strategies = new HashMap<>();
    
    static {
        strategies.put("normal", new NormalStrategy());
        strategies.put("silver", new SilverStrategy());
        strategies.put("gold", new GoldStrategy());
        strategies.put("platinum", new PlatinumStrategy());
    }
    
    public static DiscountStrategy getStrategy(String memberLevel) {
        DiscountStrategy strategy = strategies.get(memberLevel);
        if (strategy == null) {
            throw new IllegalArgumentException("不支持的会员等级: " + memberLevel);
        }
        return strategy;
    }
}

// 优化后的折扣服务
public class OptimizedDiscountService {
    public double calculateDiscount(String memberLevel, double amount) {
        DiscountStrategy strategy = DiscountStrategyFactory.getStrategy(memberLevel);
        return strategy.calculate(amount);
    }
}